<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Price_Number extends ORM
{
    /**
     * @param $params
     * @return array
     * 设置总库存
     */
    public static function set_product_number($params)
    {
        $table_field = St_Functions::table_field($params['type_id']);
        $number_table_name = $table_field[4];
        $suit_table_name = $table_field[0];
        $week_val = $params['week_val'];
        $month_val = $params['month_val'];

        $product_id = DB::select()->from($suit_table_name)->where("id={$params['suit_id']}")->execute()->get($table_field[1]);
        $day_num = (strtotime($params['end_time'])-strtotime($params['start_time']))/86400+1;
        $to_day = strtotime($params['start_time']);

        try {
            for ($i = 0; $i < $day_num; $i++) {
                if (count($week_val) > 0 && $params['number_rule']==2) {
                    $week = date('w', $to_day);
                    $week = $week == 0 ? 7 : $week;
                    if (!in_array($week, $week_val)) {
                        $to_day += 86400;
                        continue;
                    }
                }
                if(count($month_val) > 0 && $params['number_rule']==3){
                    $day = date('j', $to_day);
                    if (!in_array($day, $month_val)) {
                        $to_day += 86400;
                        continue;
                    }
                }
                $model = ORM::factory($number_table_name);
                $model->where('product_id','=',$product_id);
                $model->and_where('suit_id','=',$params['suit_id']);
                $model->and_where('day','=',$to_day);
                $model->find();
                if ($model->id) {
                    $model->number = $params['number'];
                } else {
                    $model->product_id = $product_id;
                    $model->suit_id = $params['suit_id'];
                    $model->day = $to_day;
                    $model->number = $params['number'];
                }
                $model->save();
                if (!$model->saved()) {
                    throw new Exception($model->error);
                }
                $to_day += 86400;
            }
            $res = [
                'status' => true,
                'msg'    => '设置成功',
            ];
        } catch (Exception $e) {
            $res = [
                'status' => false,
                'msg'    => $e->getMessage(),
            ];
        }

        return $res;
    }

    /**
     * @param $params
     * @return array
     * 修改对应日期的库存
     */
    public static function set_product_single_number($params)
    {
        $table_field = St_Functions::table_field($params['type_id']);
        $day = strtotime($params['date']);
        $suit = DB::select()->from($table_field[0])->where('id','=',$params['suit_id'])->execute()->current();
        $product_id = $suit[$table_field[1]];

        $exists = DB::select()->from($table_field[4])
          ->where('suit_id', '=', $params['suit_id'])
          ->where('product_id', '=', $product_id)
          ->where('day', '=', $day)
          ->execute()->current();
        if($exists){
            $result = DB::update($table_field[4])->set(array('number'=>$params['number']))->where("suit_id={$params['suit_id']} and day={$day}")->execute();
            if($table_field[6] && $params['number']!='-1'){
                $result = DB::update($table_field[6])->set(array('number'=>$params['number']))->where("{$table_field[5]}={$params['suit_id']} and day={$day}")->execute();
            }
        }else{
            $data=[
                'product_id'=>$product_id,
                'suit_id'=>$params['suit_id'],
                'day'=>$day,
                'number'=>$params['number'],
            ];
            $result = DB::insert($table_field[4],array_keys($data))->values(array_values($data))->execute();
        }


        return array(
            'status' => $result || $result===0?true:false,
            'msg'    => $result || $result===0?'设置成功':'设置失败',
            'number' => $params['number'],
            'date'   => $params['date'],
        );
    }
}